#include "cirSinglyListOps.h"

int main(int argc, char const *argv[]) {
    CirSglList circularList;
    initList(&circularList);

    short select, menuDisplayMode = 0;
    // 显示菜单
    while (1) {
        printf("\n");
        if (menuDisplayMode == 0) {
            printf("******************************************\n");
            printf("*                                        *\n");
            printf("*       ++++++++++++++++++++++++++       *\n");
            printf("*       ++    Welcome to use    ++       *\n");
            printf("*       ++  CirSglList program! ++       *\n");
            printf("*       ++++++++++++++++++++++++++       *\n");
            printf("*                                        *\n");
            printf("*     [1]  Display the list.             *\n");
            printf("*     [2]  Insert at the end.            *\n");
            printf("*     [3]  Insert at the beginning.      *\n");
            printf("*     [4]  Insert to an ordered list.    *\n");
            printf("*     [5]  The lenth of the list.        *\n");
            printf("*     [6]  Find in list.                 *\n");
            printf("*     [7]  Delete the last data.         *\n");
            printf("*     [8]  Delete the first data.        *\n");
            printf("*     [9]  Delete data by data.          *\n");
            printf("*     [10] Sort list.                    *\n");
            printf("*     [11] Reverse the list.             *\n");
            printf("*     [12] Clear list.                   *\n");
            printf("*     [13] Destroy list.                 *\n");
            printf("*                                        *\n");
            printf("*     [-1] Menu display function name.   *\n");
            printf("*     [0] Quit program.                  *\n");
            printf("*                                        *\n");
            printf("******************************************\n");
        } else {
            printf("******************************************\n");
            printf("*                                        *\n");
            printf("*       ++++++++++++++++++++++++++       *\n");
            printf("*       ++    Welcome to use    ++       *\n");
            printf("*       ++  CirSglList program! ++       *\n");
            printf("*       ++++++++++++++++++++++++++       *\n");
            printf("*                                        *\n");
            printf("*       [1]  displayList                 *\n");
            printf("*       [2]  insertAtTheEnd              *\n");
            printf("*       [3]  insertAtTheBeginning        *\n");
            printf("*       [4]  insertToOrderedList         *\n");
            printf("*       [5]  getLenth                    *\n");
            printf("*       [6]  findInList                  *\n");
            printf("*       [7]  deleteLastData              *\n");
            printf("*       [8]  deleteFirstData             *\n");
            printf("*       [9]  deleteByData                *\n");
            printf("*       [10] sortList                    *\n");
            printf("*       [11] reverseList                 *\n");
            printf("*       [12] clearList                   *\n");
            printf("*       [13] destroyList                 *\n");
            printf("*                                        *\n");
            printf("*       [-1] Menu display friendly.      *\n");
            printf("*       [0]  Quit program.               *\n");
            printf("*                                        *\n");
            printf("******************************************\n");
        }

        printf("\n You select: ");
        scanf("%hd", &select);
        if (select == 0) {
            exit(0);
        }
        printf("\x1b[H\x1b[2J");    // clear screen
        eatline;

        switch (select) {
            case 1: {
                displayList(&circularList);
                break;
            }
            case 2: {
                DataType insertData;
                int retVar;
                printf("Input data to insert (One value one line, finish with 'q'):\n");
                while (retVar = scanf("%d", &insertData), eatline, retVar == 1) {
                    if (insertAtTheEnd(&circularList, insertData) == 0) {
                        printf("Successful inserted!\n");
                    } else {
                        printf("Failed to insert.\n");
                    }
                }
                break;
            }
            case 3: {
                DataType insertData;
                int retVar;
                printf("Input data to insert (One value one line, finish with 'q'):\n");
                while (retVar = scanf("%d", &insertData), eatline, retVar == 1) {
                    if (insertAtTheBeginning(&circularList, insertData) == 0) {
                        printf("Successful inserted!\n");
                    } else {
                        printf("Failed to insert.\n");
                    }
                }
                break;
            }
            case 4: {
                char choice;
                printf("Warning: You should make sure the data in list have ordered before use this function!\nOr unexpected results may occur!\n\nAre you want to continue? (y or n): ");
                scanf("%c", &choice);
                eatline;
                if (choice == 'y' || choice == 'Y') {
                    DataType insertData;
                    printf("\nInput the data you want to insert: ");
                    scanf("%d", &insertData);
                    if (insertToOrderedList(&circularList, insertData) == 0) {
                        printf("Successful inserted!\n");
                    } else {
                        printf("Failed to insert.\n");
                    }
                }
                break;
            }
            case 5: {
                isEmpty(&circularList) ? printf("This list is empty.\n") : printf("This list has %d element(s).\n", getLenth(&circularList));
                break;
            }
            // case 6: {
            //     DataType findData;
            //     Node *retVal;
            //     printf("Input the data you want to find: ");
            //     scanf("%d", &findData);
            //     if ((retVal = findInList(&circularList, findData)) == NULL) {
            //         printf("\nNo result.\n");
            //     } else {
            //         printf("\nThe data is in the list.\nIt's at %p.\n", retVal);
            //     }
            //     break;
            // }
            case 7: {
                short retVal;
                if ((retVal = deleteLastData(&circularList)) == 0) {
                    printf("Successful deleted the last data!\n");
                } else if (retVal == -1) {
                    printf("Nothing to delete because of empty list.\n");
                } else {
                    printf("Failed to delete the last data.\n");
                }
                break;
            }
            case 8: {
                short retVal;
                if ((retVal = deleteFirstData(&circularList) == 0)) {
                    printf("Successful deleted the first data!\n");
                } else if (retVal == -1) {
                    printf("Nothing to delete because of empty list.\n");
                } else {
                    printf("Failed to delete the first data.\n");
                }
                break;
            }
            // case 9: {
            //     if (isEmpty(&circularList) == 0) {
            //         printf("Nothing to do with an empty list.\n");
            //         break;
            //     }
            //     DataType deleteData;
            //     short retVal;
            //     printf("Input the data you want to delete: ");
            //     scanf("%d", &deleteData);
            //     if ((retVal = deleteByData(&circularList, deleteData)) == 0) {
            //         printf("Successful deleted data!\n");
            //     } else if (retVal == -1) {
            //         printf("The data you want to delete is not in the list.\n");
            //     } else {
            //         printf("Failed to delete data.\n");
            //     }
            //     break;
            // }
            // case 10: {
            //     if (sortList(&circularList) == 0) {
            //         printf("Successful sorted list!\n");
            //     } else {
            //         printf("Nothing to do with an empty list.\n");
            //     }
            //     break;
            // }
            // case 11: {
            //     if (reverseList(&circularList) == 0) {
            //         printf("Successful reversed list!\n");
            //     } else {
            //         printf("Nothing to do with an empty list.\n");
            //     }
            //     break;
            // }
            // case 12: {
            //     if (clearList(&circularList) == 0) {
            //         printf("List cleared!\n");
            //     } else {
            //         printf("The list has already cleared.\n");
            //     }
            //     break;
            // }
            // case 13: {
            //     printf("Warning: After destroying the list, you will not be able to do any operations on it!\n\nAre you want to continue? (y or n): ");
            //     char choice;
            //     choice = getchar();
            //     if (choice == 'y' || choice == 'Y') {
            //         destroyList(&circularList);
            //     }
            //     break;
            // }
            case -1: {
                (menuDisplayMode == 0) ? (menuDisplayMode = 1) : (menuDisplayMode = 0);
                break;
            }
            default: {
                printf("Wrong selection. Please retry.\n");
                break;
            }
        }
    }

    // 摧毁操作不应由用户手动执行, 而应在程序执行完毕自动执行
    // destroyList(&circularList);

    return 0;
}